package com.cytech.cloudclinic.moudle.main.patient.detail;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.moudle.main.clinic.ClinicFragment;
import com.cytech.cloudclinic.moudle.main.drug.DrugManagerFragment;
import com.cytech.cloudclinic.moudle.main.patient.detail.base.PatientDetailBaseFragment;
import com.cytech.cloudclinic.moudle.main.patient.detail.caseinfo.PatientDetailCaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PatientDetailActivity extends BaseActivity {

    @BindView(R.id.titlebar_title)
    TextView titleTv;
    @BindView(R.id.fl_patient_detail_container)
    FrameLayout container;
    @BindView(R.id.rg_patient_detail)
    RadioGroup radioGroup;

    private int currentFragmentIndex = 0;
    private List<Fragment> fragmentList;
    private Fragment baseInfoFragment;
    private Fragment caseInfoFragment;
    private Fragment currentFragment;


    public static void actionStart(Context context,int id,String title){
        Intent intent = new Intent(context,PatientDetailActivity.class);
        intent.putExtra("id",id);
        intent.putExtra("title",title);
        context.startActivity(intent);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_patient_detail;
    }

    @Override
    protected void initView() {
        titleTv.setText(getIntent().getStringExtra("title"));
        int patientId = getIntent().getIntExtra("id",0);
        fragmentList = new ArrayList<>();
        baseInfoFragment = PatientDetailBaseFragment.newInstance(patientId);
        caseInfoFragment = PatientDetailCaseFragment.newInstance(patientId);
        fragmentList.add(baseInfoFragment);
        fragmentList.add(caseInfoFragment);

        switchFragment(0);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_patient_detail_base:
                        switchFragment(0);
                        break;
                    case R.id.rb_patient_detail_case:
                        switchFragment(1);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.titlebar_back)
    public void exit(View v){
        finish();
    }

    private void switchFragment(int position) {
        currentFragmentIndex = position;
        Fragment targetFragment = fragmentList.get(position);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.fl_patient_detail_container, targetFragment).commit();
            //System.out.println("还没添加呢");
        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment)
                    .commit();
            //System.out.println("添加了( ⊙o⊙ )哇");
        }
        currentFragment = targetFragment;
    }

}
